package cn.fuxi2022.module.contact.controller;

import cn.fuxi2022.core.exception.SmartGroupException;
import cn.fuxi2022.core.utils.JwtUtils;
import cn.fuxi2022.model.empty.applets.GroupContact;
import cn.fuxi2022.model.empty.applets.GroupStudentInfo;
import cn.fuxi2022.model.resp.R;
import cn.fuxi2022.module.contact.service.GroupContactService;
import cn.fuxi2022.module.info.service.GroupStudentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user/contact")
public class contactController {

    @Autowired
    private GroupContactService groupContactService;

    @Autowired
    private GroupStudentInfoService groupStudentInfoService;


    private GroupStudentInfo check(HttpServletRequest request) {
        // 判断用户阶段
        String id = JwtUtils.getMemberIdByJwtToken(request);
        LambdaQueryWrapper<GroupStudentInfo> infoWrapper = new LambdaQueryWrapper<>();
        infoWrapper.eq(GroupStudentInfo::getStudentId, id);
        GroupStudentInfo studentInfo = groupStudentInfoService.getOne(infoWrapper);
        // 不是阶段2（加群阶段）或阶段3（完成阶段），就是非法的。
        if (studentInfo.getIsStudent() != 2 && studentInfo.getIsStudent() != 3) {
            throw new SmartGroupException("非法访问，请检查是否完成信息录入或支付");
        }
        return studentInfo;
    }

    @GetMapping("/getAccountsList")
    public R getAccountsList(HttpServletRequest request) {
        // 获取使用的微信公众号二维码
        LambdaQueryWrapper<GroupContact> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GroupContact::getStatus, 0)
                .eq(GroupContact::getType, 4)
                .orderByDesc(GroupContact::getGmtCreate);

        return R.ok().data(groupContactService.list(wrapper));
    }

    /**
     * 列表
     */
    @GetMapping("/getList")
    public R getList(HttpServletRequest request) {
        check(request);

        // 获取所有的联系
        LambdaQueryWrapper<GroupContact> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GroupContact::getStatus, 0).orderByDesc(GroupContact::getGmtCreate);

        List<GroupContact> list = groupContactService.list(wrapper);

        Map<String, List<GroupContact>> data = new HashMap();

        // 1.筛选群聊
        List<GroupContact> type1List = list.stream().filter(groupContact -> groupContact.getType() == 1).collect(Collectors.toList());
        // 2.筛选个人
        List<GroupContact> type2List = list.stream().filter(groupContact -> groupContact.getType() == 2).collect(Collectors.toList());

        // 加入集合
        data.put("type1", type1List);
        data.put("type2", type2List);

        return R.ok().data(data);
    }

    @GetMapping("/success")
    public R success(HttpServletRequest request) {
        GroupStudentInfo studentInfo = check(request);
        // 将用户阶段更正为完成
        studentInfo.setIsStudent(3);
        groupStudentInfoService.updateById(studentInfo);
        return R.ok();
    }
}
